<?php
require('config.php');
$time2=date('Y-m');
$destination_folder="../Upload/shops/".$time2.'/'; //上传文件路径 
$destination_folder2="Upload/shops/".$time2.'/'; //存数据库路径 

$sum=0;
$src=array();
if(isset($_POST["file1"])) $sum=1;
if(isset($_POST["file2"])) $sum=2;
if(isset($_POST["file3"])) $sum=3;
for($i=0;$i<$sum;$i++)
{
    $base64 = $_POST["file".$i]; // 得到参数
    $img = base64_decode($base64); // 将格式为base64的字符串解码
    $path = md5(uniqid(rand())).".jpg"; // 产生随机唯一的名字作为文件名
    $src_lin=$destination_folder.$path;
    file_put_contents($src_lin, $img); // 将图片保存到相应位置
    $src[$i]=$destination_folder2.$path;
}
for($i=0;$i<3;$i++)
{
    if($i<$sum) ;
    else $src[$i]='';
}
        $src1=$src[0];
        $src2=$src[1];
        $src3=$src[2];
        $user_id=$_REQUEST['user_id'];
        $shopName=$_REQUEST['shopName'];
        $sheng=$_REQUEST['sheng'];
        $shi=$_REQUEST['shi'];
        $qu=$_REQUEST['qu'];
        $shopAddress=$_REQUEST['shopAddress'];
        $xxAddress=$_REQUEST['xxAddress'];
        $name=$_REQUEST['name'];
        $phone=$_REQUEST['phone'];
        $email=$_REQUEST['email'];
        $yyzz=$_REQUEST['yyzz'];
        $id_number=$_REQUEST['id_number'];
        // $src1=$_REQUEST['src1'];
        // $src2=$_REQUEST['src2'];
        // $src3=$_REQUEST['src3'];
        $lon=$_REQUEST['lon'];
        $lat=$_REQUEST['lat'];


        $sql="select * from `".$oto."_areas` where `areaName` LIKE '%$sheng%' and areaFlag='1' and parentId='0'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$sheng_info[]=$row;//将取得的所有数据赋值给person_info数组
		}
		$sql="select * from `".$oto."_areas` where `areaName` LIKE '%$shi%' and areaFlag='1' and parentId!='0'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$shi_info[]=$row;//将取得的所有数据赋值给person_info数组
		}
		$sql="select * from `".$oto."_areas` where `areaName` LIKE '%$qu%' and areaFlag='1' and parentId!='0'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$qu_info[]=$row;//将取得的所有数据赋值给person_info数组
		}
		if(isset($sheng_info)) $sheng_id=$sheng_info[0]['areaId'];
		else $sheng_id=0;
		if(isset($shi_info)) $shi_id=$shi_info[0]['areaId'];
		else $shi_id=0;
		if(isset($qu_info)) $qu_id=$qu_info[0]['areaId'];
		else $qu_id=0;
		$sheng=$sheng_id;
		$shi=$shi_id;
		$qu=$qu_id;
		file_put_contents("tsxx_sd_for_ios.txt", "\r\n接口调用时间：".date('Y-m-d H:i:s')."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\n收到".$sum."张图片\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\n分别为：".json_encode($src)."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\nuser_id:".$user_id."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\nshopName:".$shopName."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\nsheng:".$sheng."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\nshi:".$shi."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\nqu:".$qu."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\nshopAddress:".$shopAddress."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\nxxAddress:".$xxAddress."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\nname:".$name."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\nphone:".$phone."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\nemail:".$email."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\nyyzz:".$yyzz."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\nid_number:".$id_number."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\nlon:".$lon."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\nlat:".$lat."\r\n", FILE_APPEND);

		$sql="select * from `".$oto."_shops` where `userId`='{$user_id}' and shopFlag='1'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$shops_info[]=$row;//将取得的所有数据赋值给person_info数组
		}
		if(!isset($shops_info))
		{
			$shopSn=time();
			$time=date('Y-m-d H:i:s');
			$sql="INSERT INTO ".$oto."_shops (`shopSn`,`userId`,`areaId1`,`areaId2`,`areaId3`,`shopName`,`shopTel`,`shopAddress`,`createTime`,`latitude`,`longitude`,`idNo`,`idImg`,`idRevImg`,`permitImg`,`permitNo`) VALUES ('$shopSn','$user_id','$sheng','$shi','$qu','$shopName','$phone','$xxAddress','$time','$lat','$lon','$id_number','$src2','$src3','$src1','$yyzz')";
            $result=$db->query($sql);
            //file_put_contents("tsxx.txt", "\r\n".$sql."\r\n", FILE_APPEND);
            if($result)
            {
            	$data[0]['pan']=1;
				$data[0]['msg']='提交成功，请等待审核！';
            }
            else
            {
            	$data[0]['pan']=-1;
				$data[0]['msg']='提交失败，请稍后再试！';
            }
		}
		else
		{
			if($shops_info[0]['shopStatus']<0)
			{
				$shop_id=$shops_info[0]['shopId'];
				$sql="update ".$oto."_shops set areaId1='{$sheng}',areaId2='{$shi}',areaId3='{$qu}',shopName='{$shopName}',shopTel='{$phone}',shopAddress='{$xxAddress}',shopStatus='0',idNo='{$id_number}',idImg='{$src2}',idRevImg='{$src3}',permitImg='{$src1}',premitNo='{$yyzz}' where shopId='{$shopId}'";
				$result=$db->query($sql);
				//file_put_contents("tsxx.txt", "\r\n".$sql."\r\n", FILE_APPEND);
				if($result)
				{
					$data[0]['pan']=1;
					$data[0]['msg']='提交成功，请等待审核！';
				}
				else
				{
					$data[0]['pan']=-1;
					$data[0]['msg']='提交失败，请稍后再试！';
				}
			}
			else
			{
				if($shops_info[0]['shopStatus']==0)
				{
					$data[0]['pan']=-1;
					$data[0]['msg']='资料已提交，请等待审核！';
				}
				else if($shops_info[0]['shopStatus']==1)
				{
					$data[0]['pan']=-1;
					$data[0]['msg']='您已经是商家了！';
				}
				else
				{
					$data[0]['pan']=-1;
					$data[0]['msg']='暂时不能申请入驻，请联系平台管理';
				}
			}
		}
		echo json_encode($data);
?>